package com.qf.controller;

import com.qf.pojo.PageBean;
import com.qf.pojo.Product;
import com.qf.pojo.User;
import com.qf.service.ProductService;
import com.qf.service.impl.ProductServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * 商品的后端接口地址
 */
@WebServlet("/product")
public class ProductServlet extends BaseServlet {

    /**
     * 获取指定的商品
     * @param request 请求对象
     * @param response 响应对象
     */
    public  void getProduct(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //${pageContext.request.contextPath}/product?methoddName=getProduct&pid=${product.pid}

        //从request域中获取HttpSession
        HttpSession session = request.getSession();
        //从HttpSession先获取"user"里面的用户信息
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        if(user==null){
            //必须走登录页面
            //请求转发到登录
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);

        }

        ////1)获取参数pid
        String pid = request.getParameter("pid");
        System.out.println(pid);
        //2)调用ProductService 商品的业务接口
        ProductService productService = new ProductServiceImpl() ;
        Product product = productService.getProductByPid(pid);
        //3)判断
        if(product!=null){
            //存储在request域中
            request.setAttribute("product",product) ;
            //请求转发到商品详情页
            request.getRequestDispatcher("/jsp/product_info.jsp").forward(request,response);
        }

    }


    /**
     * 前台点击商品分类进行商品分页获取
     * @param request  请求对象
     * @param response 响应对象
     */
    public void getProdcutByPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //request对象获取HttpSession,判断session中是否"登录的user信息",获取user实体null,请求转发到登录页面

        //<a href='${pageContext.request.contextPath}/product?methodName=getProdcutByPage&cid="+this.cid+"
        //获取商品的分类cid
        String cid = request.getParameter("cid");
        //System.out.println("商品分类id是:"+cid);

        //获取前端地址栏上的当前页码currentPage/pageSize
        String curPage = request.getParameter("currentPage");
        if(curPage==null || "".equals(curPage)){
            curPage = "1" ;//默认第一页
        }
        String pSize = request.getParameter("pageSize");
        if(pSize== null || "".equals(pSize)){
            //一页显示12条
            pSize = "12" ;
        }
        //将curePage和pSize转成int
        int currentPage = Integer.parseInt(curPage) ;
        int pageSize = Integer.parseInt(pSize) ;

        //调用商品的业务接口
        ProductService productService = new ProductServiceImpl() ;
        PageBean<Product> pb = productService.getProductByPage(cid, currentPage, pageSize);
        if(pb!=null){
            //存储在request域中
            request.setAttribute("pb",pb) ;
            //请求转发到/jsp/product_list.jsp
            request.getRequestDispatcher("/jsp/product_list.jsp").forward(request,response);

        }

    }

    public  void addProduct(HttpServletRequest request,HttpServletResponse response){

        //创建HashMap
        HashMap<String,Object>  map = new HashMap<>() ;

        //创建磁盘文件
        DiskFileItemFactory factory = new DiskFileItemFactory() ;
        //创建文件上传核心对象
        ServletFileUpload upload = new ServletFileUpload(factory) ;

        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            for (FileItem fileItem : fileItems) {
                if(fileItem.isFormField()){
                    //普通组件
                    map.put(fileItem.getFieldName(),fileItem.getString("utf-8")) ;
                }else{
                    //文件上传组件
                    String name = fileItem.getName();

                    //获取文件的真实名称
                    UploadUyil
                }
            }
        } catch (FileUploadException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

}
